//
//  CZQStudentExeView.m
//  coachmanage
//
//  Created by 众拳科技 on 2018/3/20.
//  Copyright © 2018年 众拳科技. All rights reserved.
//

#import "CZQStudentExeView.h"
#import "DHCustomImageView.h"
@interface CZQStudentExeView ()<SDPhotoBrowserDelegate>
{
    //前还是后
    BOOL _isBefor;
}
@end
@implementation CZQStudentExeView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        UIScrollView *scrollView = [[UIScrollView alloc] init];
        scrollView.backgroundColor = CSWhiteColor;
        [self addSubview:scrollView];
        self.scrollView = scrollView;
        
        //头像等
        
        UIButton *icon = [UIButton buttonWithType:UIButtonTypeCustom];
        [icon setImage:PlaceHold forState:UIControlStateNormal];
        icon.backgroundColor = CSColorFromRGB(0x666666);
        icon.layer.cornerRadius = 25;
        icon.clipsToBounds = YES;
        [self.scrollView addSubview:icon];
        self.iconBtn = icon;
        
        UILabel *iconLabel = [[UILabel alloc] init];
        iconLabel.text = @"头像";
        iconLabel.font = MidBigFont;
        iconLabel.textColor = CSColorFromRGB(0x666666);
        [self.scrollView addSubview:iconLabel];
        self.iconLable = iconLabel;
        
        UIView *iconLine = [UIView new];
        iconLine.backgroundColor = BackGroundColor;
        [self.scrollView addSubview:iconLine];
        self.iconLineView = iconLine;
        
        //标签等
        CommonBtnView *longBtnView = [[CommonBtnView alloc] init];
        longBtnView.titleLabel.text = @"标签";
        longBtnView.choseLabel.textColor = PlaceHoldColor;
        longBtnView.rightImageView.image = [UIImage imageNamed:@"icon_add"];
//        longBtnView.lineView.hidden = YES;
        [scrollView addSubview:longBtnView];
        self.labBtnView = longBtnView;
        
        UIScrollView *labScrollView = [[UIScrollView alloc] init];
        [self.scrollView addSubview:labScrollView];
        self.labScrollView = labScrollView;
        
        UIView *labLineView = [UIView new];
        labLineView.backgroundColor = BackGroundColor;
        [self.scrollView addSubview:labLineView];
        self.labLineView = labLineView;
        
        //健身成果等
        CommonInputView *sportView = [[CommonInputView alloc] init];
        sportView.titleTF.userInteractionEnabled = NO;
        sportView.titleLabel.text = @"健身成果";
        [self.scrollView addSubview:sportView];
        self.sportInputView = sportView;
        
        CZQCustonTextView *sportTextView = [CZQCustonTextView new];
        sportTextView.placeLable.text = @"一句话介绍";
        [self.scrollView addSubview:sportTextView];
        self.sportReportView = sportTextView;
        
        UIView *sportLineView = [UIView new];
        sportLineView.backgroundColor = BackGroundColor;
        [self.scrollView addSubview:sportLineView];
        self.sportLineView = sportLineView;
        
        //健身前照片等
        CommonInputView *sportafterView = [[CommonInputView alloc] init];
        sportafterView.titleTF.userInteractionEnabled = NO;
        sportafterView.titleLabel.text = @"健身前照片";
        [self.scrollView addSubview:sportafterView];
        self.sportAfterInputView = sportafterView;
        
        UIScrollView *sportafterScrolView = [UIScrollView new];
        [self.scrollView addSubview:sportafterScrolView];
        self.sportAfterScrollView = sportafterScrolView;
        
        UIButton *addAfterBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [addAfterBtn setImage:@"icon_addimg"];
        [self.sportAfterScrollView addSubview:addAfterBtn];
        self.sportAfterBtn = addAfterBtn;
        
        
        UIView *sportAfterLineView = [UIView new];
        sportAfterLineView.backgroundColor = BackGroundColor;
        [self.scrollView addSubview:sportAfterLineView];
        self.sportAfterLineView = sportAfterLineView;
        
        
        //健身后照片等
        CommonInputView *sportBefourView = [[CommonInputView alloc] init];
        sportBefourView.titleTF.userInteractionEnabled = NO;
        sportBefourView.titleLabel.text = @"健身后照片";
        [self.scrollView addSubview:sportBefourView];
        self.sportBefourInputView = sportBefourView;
        
        UIScrollView *sportBefourScrolView = [UIScrollView new];
        [self.scrollView addSubview:sportBefourScrolView];
        self.sportBefourScrollView = sportBefourScrolView;
        
        UIButton *addbefourBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [addbefourBtn setImage:@"icon_addimg"];
        [self.sportBefourScrollView addSubview:addbefourBtn];
        self.sportBefourBtn = addbefourBtn;
        
        UIButton *sureBtn = [[UIButton alloc] init];
        [sureBtn setTitle:@"删除"];
        sureBtn.hidden = YES;
        [sureBtn setTitleColor:CSColorFromRGB(0x0a0a0a)];
        sureBtn.titleLabel.font = MidBigFont;
        sureBtn.backgroundColor = ButtonBackGroundColor;
        [self.scrollView addSubview:sureBtn];
        self.deletBtn = sureBtn;
        
        [self setConstraints];
        
    }
    return self;
}

-(void)setConstraints{
    
    [self.scrollView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.edges.mas_equalTo(self);
    }];
    
    //头像等
    
    [self.iconBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.scrollView).offset(10);
        
        make.right.mas_equalTo(self).offset(-15);
        
        make.bottom.mas_equalTo(self.iconLineView.mas_top).offset(-10);
        
        make.width.height.mas_equalTo(50);
        
    }];
    
    [self.iconLable mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(self.scrollView).offset(10);
        
        make.centerY.mas_equalTo(self.iconBtn);
        
        
    }];
    
    [self.iconLineView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.left.width.mas_equalTo(self.scrollView);
        
        make.bottom.mas_equalTo(self.labBtnView.mas_top);
        
        make.height.mas_equalTo(10);
        
        
    }];
    
    //标签等
    [self.labBtnView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.left.width.mas_equalTo(self.scrollView);
        
        make.bottom.mas_equalTo(self.labScrollView.mas_top);
        
        
    }];
    
    [self.labScrollView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.left.width.mas_equalTo(self.scrollView);
        
        make.bottom.mas_equalTo(self.labLineView.mas_top);
        
        
    }];
    
    [self.labLineView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.left.width.mas_equalTo(self.scrollView);
        
        make.bottom.mas_equalTo(self.sportInputView.mas_top);
        
        make.height.mas_equalTo(10);
        
        
    }];
    
    //健身成果等
    [self.sportInputView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.left.width.mas_equalTo(self.scrollView);
        
        make.bottom.mas_equalTo(self.sportReportView.mas_top);
        
        
    }];
    
    [self.sportReportView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.left.width.mas_equalTo(self.scrollView);
        
        make.bottom.mas_equalTo(self.sportLineView.mas_top);
        
        make.height.mas_equalTo(150);
    }];
    
    [self.sportLineView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.left.width.mas_equalTo(self.scrollView);
        
        make.bottom.mas_equalTo(self.sportAfterInputView.mas_top);
        
        make.height.mas_equalTo(10);
    }];
    
    //健身前照片等
    [self.sportAfterInputView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.left.width.mas_equalTo(self.scrollView);
        
        make.bottom.mas_equalTo(self.sportAfterScrollView.mas_top);
        
        
        
    }];
    
    
    
    [self.sportAfterBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.left.top.mas_equalTo(self.sportAfterScrollView).offset(10);
        
        make.bottom.mas_equalTo(self.sportAfterScrollView).offset(-10);
        
        make.width.height.mas_equalTo(50);
        
    }];
    
    [self.sportAfterScrollView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.left.width.mas_equalTo(self.scrollView);
        
        make.bottom.mas_equalTo(self.sportAfterLineView.mas_top);
        
        make.width.height.mas_equalTo(70);
        
    }];
    
    [self.sportAfterLineView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.left.width.mas_equalTo(self.scrollView);
        
        make.bottom.mas_equalTo(self.sportBefourInputView.mas_top);
        
        make.height.mas_equalTo(10);
        
    }];
    
    
    //健身后照片等
    
    [self.sportBefourInputView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.left.width.mas_equalTo(self.scrollView);
        
        make.bottom.mas_equalTo(self.sportBefourScrollView.mas_top);
        
    }];
    [self.sportBefourBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.left.top.mas_equalTo(self.sportBefourScrollView).offset(10);
        
        make.bottom.mas_equalTo(self.sportBefourScrollView).offset(-10);
        
        make.width.height.mas_equalTo(50);
        
    }];
    
    [self.sportBefourScrollView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.left.width.mas_equalTo(self.scrollView);
        
        
        make.width.height.mas_equalTo(70);
        
    }];
    
    [self.deletBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.sportBefourScrollView.mas_bottom).offset(30);
        
//        make.bottom.mas_equalTo(self).offset(-20);
        
        make.left.mas_equalTo(self.scrollView).offset(20);
        
        make.right.mas_equalTo(self).offset(-20);
        
        make.height.mas_equalTo(50);
        
    }];
    
    
    
    
}
#pragma mark - //添加了健身h后图片

-(void)setSportBeforArrM:(NSMutableArray *)sportBeforArrM
{
    _sportBeforArrM = sportBeforArrM;
    
    for (UIView *view in self.sportBefourScrollView.subviews) {
        
        if ([view isKindOfClass:[UIImageView class]]) {
            [view removeFromSuperview];
        }
        
    }
    
    //记录前一个view
    UIImageView *imgView = nil;
    
    for (int i = 0; i < sportBeforArrM.count; i ++) {
    
        DHCustomImageView *img = [DHCustomImageView new];
        
    
        
        img.tag = 1000 + i;
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(showBefor:)];
        
        [img addGestureRecognizer:tap];
        
        img.deleteBtn.tag = 400 + i;
        
        [img.deleteBtn addTarget:self action:@selector(deleteBefor:)];
        
        [img sd_setImageWithURL:[Helper imageUrlStringToChinese:sportBeforArrM[i] imageType:nil] placeholderImage:PlaceHold];
        [self.sportBefourScrollView addSubview:img];
        
        
        if (i == 0) {
            
            [img mas_remakeConstraints:^(MASConstraintMaker *make) {
                
                make.top.left.mas_equalTo(img.superview).offset(10);
                make.bottom.mas_equalTo(img.superview).offset(-10);
                make.width.height.mas_equalTo(50);
                
            }];
            
        }else if (i == sportBeforArrM.count - 1){
            
            [img mas_remakeConstraints:^(MASConstraintMaker *make) {
                
                make.top.mas_equalTo(img.superview).offset(10);
                make.left.mas_equalTo(imgView.mas_right).offset(10);
                make.bottom.mas_equalTo(img.superview).offset(-10);
                make.right.mas_equalTo(self.sportBefourBtn.mas_left).offset(-10);
                make.width.height.mas_equalTo(50);
                
            }];
            
        }else
        {
            [img mas_remakeConstraints:^(MASConstraintMaker *make) {
                
                make.top.mas_equalTo(img.superview).offset(10);
                make.left.mas_equalTo(imgView.mas_right).offset(10);
                make.bottom.mas_equalTo(img.superview).offset(-10);
                make.width.height.mas_equalTo(50);
                
            }];
            
        }
        
        imgView = img;
        
    }
    
    if (sportBeforArrM.count > 0) {
        [self.sportBefourBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.top.mas_equalTo(self.sportBefourBtn.superview).offset(10);
            make.bottom.mas_equalTo(self.sportBefourBtn.superview).offset(-10);
            make.left.mas_equalTo(imgView.mas_right).offset(10);
            make.width.height.mas_equalTo(50);
            
        }];
    }else
    {
        [self.sportBefourBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.top.mas_equalTo(self.sportBefourBtn.superview).offset(10);
            make.bottom.mas_equalTo(self.sportBefourBtn.superview).offset(-10);
            make.left.mas_equalTo(self.sportBefourBtn.superview).offset(10);
            make.width.height.mas_equalTo(50);
            
        }];
    }
 
    self.sportBefourScrollView.contentSize =CGSizeMake(60 *(sportBeforArrM.count + 1), 0);
    
    NSLog(@"%ld",sportBeforArrM.count);
    
    [self.sportBefourScrollView updateConstraints];
    [self.sportBefourScrollView setNeedsFocusUpdate];
}

#pragma mark - //添加了健身前图片
-(void)setSportAfterArrM:(NSMutableArray *)sportAfterArrM
{
    
    _sportAfterArrM = sportAfterArrM;
    
    for (UIView *view in self.sportAfterScrollView.subviews) {
        
        if ([view isKindOfClass:[UIImageView class]]) {
            [view removeFromSuperview];
        }
        
    }
    
    //记录前一个view
    UIImageView *imgView = nil;
    
    for (int i = 0; i < sportAfterArrM.count; i ++) {
        
        DHCustomImageView *img = [DHCustomImageView new];
        
        img.tag = 2000 + i;
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(showAfter:)];
        
        [img addGestureRecognizer:tap];
        
        img.deleteBtn.tag = 300 + i;
        
        [img.deleteBtn addTarget:self action:@selector(deleteAfter:)];

        [img sd_setImageWithURL:[Helper imageUrlStringToChinese:sportAfterArrM[i] imageType:nil] placeholderImage:PlaceHold];
        [self.sportAfterScrollView addSubview:img];
        
        
        if (i == 0) {
            
            [img mas_remakeConstraints:^(MASConstraintMaker *make) {
                
                make.top.left.mas_equalTo(img.superview).offset(10);
                make.bottom.mas_equalTo(img.superview).offset(-10);
                make.width.height.mas_equalTo(50);
                
            }];
            
        }else if (i == sportAfterArrM.count - 1){
            
            [img mas_remakeConstraints:^(MASConstraintMaker *make) {
                
                make.top.mas_equalTo(img.superview).offset(10);
                make.left.mas_equalTo(imgView.mas_right).offset(10);
                make.bottom.mas_equalTo(img.superview).offset(-10);
                make.right.mas_equalTo(self.sportAfterBtn.mas_left).offset(-10);
                make.width.height.mas_equalTo(50);
                
            }];
            
        }else
        {
            [img mas_remakeConstraints:^(MASConstraintMaker *make) {
                
                make.top.mas_equalTo(img.superview).offset(10);
                make.left.mas_equalTo(imgView.mas_right).offset(10);
                make.bottom.mas_equalTo(img.superview).offset(-10);
                make.width.height.mas_equalTo(50);
                
            }];
            
        }
        imgView = img;
        
        
    }
    
    if (sportAfterArrM.count > 0) {
        [self.sportAfterBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.top.mas_equalTo(self.sportAfterBtn.superview).offset(10);
            make.bottom.mas_equalTo(self.sportAfterBtn.superview).offset(-10);
            make.left.mas_equalTo(imgView.mas_right).offset(10);
            make.width.height.mas_equalTo(50);
            
        }];
    }else
    {
        [self.sportAfterBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.top.mas_equalTo(self.sportAfterBtn.superview).offset(10);
            make.bottom.mas_equalTo(self.sportAfterBtn.superview).offset(-10);
            make.left.mas_equalTo(self.sportAfterBtn.superview).offset(10);
            make.width.height.mas_equalTo(50);
            
        }];
    }
    
    
    
    
    
    self.sportAfterScrollView.contentSize =CGSizeMake(60 *(self.sportAfterArrM.count + 1), 0);
    
    [self.sportAfterScrollView updateConstraints];
    [self.sportAfterScrollView setNeedsLayout];
    
}

#pragma mark - //添加了标签

-(void)setLongArrM:(NSMutableArray *)longArrM
{
    _longArrM = longArrM;
    
    [self.labScrollView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    
    //记录前一个View
    CZQLongBtnView *longView = nil;
    
    for (int i = 0 ; i < self.longArrM.count; i ++ ) {
        
        CZQLongBtnView *view = [CZQLongBtnView new];
        
        view.tag = 200 + i;
        //删除
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(removeLong:)];
        
        [view addGestureRecognizer:tap];
        
        view.longLable.text = longArrM[i];
        
        [self.labScrollView addSubview:view];
        
        CGFloat w = [Helper widthOfString:longArrM[i] font:[UIFont systemFontOfSize:15] height:30] + 50;
        
        
        if (i == 0) {
            
            [view mas_remakeConstraints:^(MASConstraintMaker *make) {
                
                make.top.bottom.mas_equalTo(view.superview);
                make.left.mas_equalTo(view.superview).offset(10);
                make.height.mas_equalTo(50);
                make.width.mas_equalTo(w);
                
                
            }];
            
        }else if (i == longArrM.count - 1){
            
            [view mas_remakeConstraints:^(MASConstraintMaker *make) {
                
                make.top.height.mas_equalTo(longView);
                make.left.mas_equalTo(longView.mas_right).offset(10);
                make.right.mas_equalTo(self.labScrollView).offset(-10);
                make.width.mas_equalTo(w);
                
            }];
            
        }else
        {
            [view mas_remakeConstraints:^(MASConstraintMaker *make) {
                
                make.top.height.mas_equalTo(longView);
                make.left.mas_equalTo(longView.mas_right).offset(10);
                make.width.mas_equalTo(w);
                
            }];
            
        }
        
        longView = view;
        
    }
    
    if (longArrM.count > 0) {
        
        [self.labScrollView mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.left.width.mas_equalTo(self.scrollView);
            
            make.bottom.mas_equalTo(self.labLineView.mas_top);
            
            make.height.mas_equalTo(50);
            
            
        }];
        
    }else
    {
        [self.labScrollView mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.left.width.mas_equalTo(self.scrollView);
            
            make.bottom.mas_equalTo(self.labLineView.mas_top);
            
            make.height.mas_equalTo(0);
        }];
        
    }
    
    self.labScrollView.contentSize = CGSizeMake(longView.right + 10, 0);
    
    [self layoutIfNeeded];
    
}
#pragma mark -删除健身后图片
-(void)deleteBefor:(UIButton *)sender
{
    
    
    [self.sportBeforArrM removeObjectAtIndex:sender.tag - 400];
    
    [self setSportBeforArrM:self.sportBeforArrM];
    
}

#pragma mark -删除健前图片
-(void)deleteAfter:(UIButton *)sender
{
    [self.sportAfterArrM removeObjectAtIndex:sender.tag - 300];
    
    [self setSportAfterArrM:self.sportAfterArrM];
    
}
#pragma mark -删除擅长
-(void)removeLong:(UITapGestureRecognizer *)tap
{
    CZQLongBtnView *view = tap.view;
    
    [self.longArrM removeObjectAtIndex:view.tag -200];
    
    [self setLongArrM:self.longArrM];
    
    if (self.longArrM.count > 0) {
        self.model.FavTags = [self.longArrM componentsJoinedByString:@","];
    }else
    {
        self.model.FavTags = @"";
    }
    
    
    
}

- (void)showAfter:(UITapGestureRecognizer *)tap
{
    UIView *imageView = tap.view;
    SDPhotoBrowser *browser = [[SDPhotoBrowser alloc] init];
    browser.currentImageIndex = imageView.tag - 2000;
    browser.sourceImagesContainerView = imageView;
    
    _isBefor = NO;
    
    browser.imageCount = self.sportAfterArrM.count;
    browser.delegate = self;
    [browser show];
    
//    browser.removeBtn.hidden = NO;
}

- (void)showBefor:(UITapGestureRecognizer *)tap
{
    _isBefor = YES;
    UIView *imageView = tap.view;
    SDPhotoBrowser *browser = [[SDPhotoBrowser alloc] init];
    browser.currentImageIndex = imageView.tag - 1000;
    browser.sourceImagesContainerView = imageView;
    
    browser.imageCount = self.sportBeforArrM.count;
    browser.delegate = self;
    [browser show];
    
//    browser.removeBtn.hidden = NO;
}


#pragma mark - SDPhotoBrowserDelegate



- (NSURL *)photoBrowser:(SDPhotoBrowser *)browser highQualityImageURLForIndex:(NSInteger)index
{
    //    _index = index;
    
    NSString *urlStr = [NSString stringWithFormat:@"%@%@",_isBefor?self.sportBeforArrM[index]:self.sportAfterArrM[index] ,mid_image];
    urlStr=[urlStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    NSURL *url = [NSURL URLWithString:urlStr];
    return url;
}

- (UIImage *)photoBrowser:(SDPhotoBrowser *)browser placeholderImageForIndex:(NSInteger)index
{
    //    UIImageView *imageView ;
    
    return PlaceHold;
}



-(void)layoutSubviews{
    
    self.scrollView.contentSize = CGSizeMake(self.width, self.deletBtn.bottom + CZQ_StatusBarHeight + CZQ_NavigationBarHeight + 20);
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
